﻿
@{
    Layout = null;
}

<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">退货单编号</label>
        <div class="layui-input-inline">
            <input type="text" id="returnCode" name="returnCode" style="width:200px" lay-verify="required" placeholder="请输入退货单编号" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label">退货单名称</label>
        <div class="layui-input-inline">
            <input type="text" id="returnName" name="returnName" style="width:200px" lay-verify="required" placeholder="请输入退货单名称" autocomplete="off" class="layui-input">
        </div>
        <div>
            <button type="button" id="btnSearch" class="layui-btn layui-btn-sm layui-bg-blue">搜索</button>
            <button type="button" class="layui-btn  layui-btn-primary  layui-btn-sm">重置</button>
            <button type="button" class="layui-btn layui-bg-blue  layui-btn-sm" onclick="Insert()">
                <i class="layui-icon layui-icon-add-1">添加</i>
            </button>
            <button type="button" id="delete" class="layui-btn layui-bg-red  layui-btn-sm" lay-event="delete">
                <i class="layui-icon layui-icon-delete">删除</i>
            </button>
        </div>
    </div>
    </form>
<script type="text/html" id="btn">
    <button type="button" class="layui-btn layui-bg-blue layui-btn-xs" lay-event="update">
        <i class="layui-icon layui-icon-edit">编辑</i>
    </button>
    <button type="button" class="layui-btn layui-btn-danger  layui-btn-xs" lay-event="delete">
        <i class="layui-icon layui-icon-delete">删除</i>
    </button>
</script>
    <table class="layui-hide" id="SupplierReturnLists" layer-filter="SupplierReturnLists"></table>
    <script src="~/libs/jquery/jquery.js"></script>
    <script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
    <script>
        layui.use('table', function () {
            var table = layui.table;
            var from = layui.from;
            var laydata = layui.laydata;
            var inst = table.render({});
            // 已知数据渲染
            function select() {
                table.render({
                    elem: '#SupplierReturnLists',
                    url: '/SupplierReturn/GetSupplierReturnList',
                    cols: [[ //标题栏
                        { type: 'checkbox', fixed: 'left' },
                        { field: 'returnCode', title: '退货单编码', width: 120 },
                        { field: 'returnName', title: '退货单名称', width: 120 },
                        { field: 'purchaseCode', title: '采购订单编码', width: 120 },
                        { field: 'supplierManagementCode', title: '供应商编码', width: 120 },
                        { field: 'supplierName', title: '供应商名称', width: 120 },
                        {
                            field: 'returnDate', title: '退货日期', width: 120,
                            templet: function (d) {
                                return d.returnDate.substring(0, 10)
                            }
                        },
                        {
                            field: 'dstatus', title: '单据状态', width: 120,
                            templet: function (d) {
                                return d.dstatus==1?'已完成':'草稿'
                            }
                        },
                        { title: '操作', width: 160, toolbar: '#btn' }
                    ]],

                    //skin: 'line', // 表格风格
                    //even: true,
                    page: true, // 是否显示分页
                    height: 500,
                    limits: [5, 10, 20, 30],
                    limit: 10, // 每页默认显示的数量
                    where: {
                        pageIndex: 1,
                        pageSize: 10
                    }
                });
            }
            select();


            //查询
            $('#btnSearch').on('click', function () {
                table.reload('SupplierReturnLists', {
                    page: { curr: 1 },
                    where: {
                        returnCode: $("#returnCode").val(),
                        returnName: $("#returnName").val()
                    }
                    
                })
              
                return false;
            })

            //删除
            // 触发单元格工具事件
            table.on('tool(SupplierReturnLists)', function (obj) { // 双击 toolDouble
                var data = obj.data; // 获得当前行数据
                console.log(obj)
                if (obj.event === 'delete') {
                    layer.confirm("确定要删除吗?", function (index) {
                        var array = []
                        array.push(data.id);
                        
                        deleteSupplierReturn(array);
                        layer.close(index)
                    })
                }
                if (obj.event === 'update') {
                    // 在此处输入 layer 的任意代码
                    layer.open({
                        type: 2, // page 层类型
                        area: ['500px', '500px'],
                        title: '编辑供应商退货',
                        shade: 0.6, // 遮罩透明度
                        shadeClose: true, // 点击遮罩区域，关闭弹层
                        anim: 0, // 0-6 的动画形式，-1 不开启
                        content: '/SupplierReturn/UpdateSupplierReturn?id=' + data.id,
                    });
                }
            });

            // 批量删除按钮点击事件
            $('#delete').on('click', function () {
                var checkStatus = table.checkStatus('SupplierReturnLists');
                var data = checkStatus.data;
                
                if (data.length === 0) {
                    layer.msg('请选择要删除的数据');
                    return;
                }
                layer.confirm('确定删除选中的 ' + data.length + ' 条数据吗？', function (index) {
                    var ids = data.map(item => item.id);
                    deleteSupplierReturn(ids);
                    
                    layer.close(index);
                });
            });
        });


        var deleteSupplierReturn = function (ids) {
            $.ajax({
                type: 'get',
                url: '/SupplierReturn/DeleteSupplierReturn',
                dataType: 'json',
                data: { ids: ids.toString() },
                success: function (res) {
                    if (res > 0) {
                        layer.msg("删除成功");
                        location.href ='/SupplierReturn/SupplierReturnList'
                    }
                }
            })
        }

        function Insert() {
            // 在此处输入 layer 的任意代码
            layer.open({
                type: 2, // page 层类型
                area: ['500px', '500px'],
                title: '添加供应商退货',
                shade: 0.6, // 遮罩透明度
                shadeClose: true, // 点击遮罩区域，关闭弹层
                anim: 0, // 0-6 的动画形式，-1 不开启
                content: '/SupplierReturn/CreateSupplierReturn'
            });
        }
    </script>



